(function() {
	
	if (!window.CJ) {
		window.CJ = {};
	}
	

	function Video () {
		this.name = 'Video';
		this.rootID = "#vod";
		this.rootElem = $(this.rootID+" .control ul");
		this.bindEvent();
	}
	Video.play = function (pID, eID, backCB) {
		this.programID = pID;
		this.episodeID = eID;
		CJ.Scene.videoPlayBackCB = backCB;
		CJ.Loading.on();
		CJ.HttpJson.get({
			type: 'EPISODESTREAMINGURL',
			param: {
				userID: CJ.plugin.membership.getUserID(),
				ProgramID: pID,
				EpisodeID: eID
			},
			success:  function (json) {
				CJ.plugin.Player.setSrc(json.streamingurl);
				Video.getLikeStatics();
				//view 포커스
				CJ.Scene.view();
				//컨트롤 자동감춤 시작
				CJ.AutoHideCtl.hide();
			},
			complete: function (){
				CJ.Loading.off();
				CJ.Content.getData(pID);//B119990553
			},
			error: function () {
				console.log('video play error : play url load fail');
				backCB();
			}
		});
	};
	Video.getLikeStatics = function () {
		CJ.Loading.on();
		CJ.HttpJson.get({
			type: 'EPISODESTATICS',
			param: {
				userID: CJ.plugin.membership.getUserID(),
				ProgramID: this.programID,
				EpisodeID: this.episodeID
			},
			success:  function (json) {
				//$('div.chart>b').before('<p>'+JSON.stringify(json.episodestatics)+'</p>');
				Video.renderLikeStatics(json);
			},
			complete: function (){
				CJ.Loading.off();
			}
		});
	};
	Video.like = function (successCB) {
		CJ.HttpJson.get({
			type: 'EPISODELIKE',
			param: {
				userID: CJ.plugin.membership.getUserID(),
				ProgramID: this.programID,
				EpisodeID: this.episodeID
			},
			success:  function (json) {
				if (typeof successCB == 'function') successCB();
			},
			complete: function (){
			}
		});
	};
	Video.renderLikeStatics = function (json) {
		var i, len, sum = 0;
		for (i=0, len=json.episoderank.length; i<len; i++) {
			sum += parseInt(json.episoderank[i].rank_count);
		}
		for (i=0, len=json.episoderank.length; i<len; i++) {
			json.episoderank[i].total = sum;
		}
		sum = 0;
		for (i=0, len=json.episodestatics.length; i<len; i++) {
			sum += parseInt(json.episodestatics[i].age_count);
		}
		for (i=0, len=json.episodestatics.length; i<len; i++) {
			json.episodestatics[i].total = sum;
		}
		$( ".chart" ).html(
			$.render.likeStaticsTemplate(json)
		);
	};
	
	Video.prototype = new EventCtl();
	Video.prototype.bindEvent = function () {
		var self = this;
		$(this.rootElem).on('click', 'li', function(evt){
			CJ.Scene["Qmenu"].removeFocus();
			CJ.Scene.setDelegate(CJ.Scene["Video"]);
			self.enter(evt);
		});
		
		$(this.rootElem).on('mouseenter', 'li', function(evt){
			CJ.Scene["Qmenu"].removeFocus();
			CJ.Scene.setDelegate(CJ.Scene["Video"]);
			
			$(this).siblings().removeClass('focus').end().addClass('focus');
		});
	};
	Video.prototype.up = function () {
		$('li.focus', this.rootElem).removeClass('focus');
		CJ.Scene.qmenu();
	};
	Video.prototype.left = function () {
		this.moveH('prev');
	};
	Video.prototype.right = function () {
		this.moveH('next');
	};
	Video.prototype.moveH = function (func) {
		var now = $('li.focus', this.rootElem),
			next = now[func]();
		if (next.length>0) {
			now.removeClass('focus');
			next.addClass('focus');
		}
	};
	Video.prototype.back = function () {
		$("li.focus", this.rootElem).removeClass('focus');
		$("li:nth-child(2)", this.rootElem).data("ctl", 'pause').html('<b>o</b>').addClass('focus');
		CJ.plugin.Player.channeling();
		CJ.Scene.videoPlayBackCB();
	};
	Video.prototype.enter = function (evt) {
		//Vod control
		var btn = (evt.type == "click") ? $(evt.currentTarget): $("li.focus", this.rootElem);
		btn.addClass('focus');
		var ctl = btn.data("ctl");
		if (ctl == 'pause') {
			btn.data("ctl", 'play').html('<b>q</b>');
		} else if (ctl == 'play') {
			btn.data("ctl", 'pause').html('<b>o</b>');
		}
		CJ.plugin.Player[ctl]();
		if (ctl == 'stop') {
			btn.removeClass('focus');
			$("li:nth-child(2)", this.rootElem).data("ctl", 'pause').html('<b>o</b>').addClass('focus');
			this.back();
		}
	};
	CJ.Video = Video;

	
	$.extend(CJ.Scene, {
							//처음 비디오 화면으로 이동시
							view: function(){
								$(this["Qmenu"].rootID+" .menu").removeClass("hover m1 m2 m3 m4") //말풍선 제거
								.find('ul>li').removeClass('on focus').eq(0).addClass('on focus'); //VOD control 선택
								$("#vod").removeClass().addClass('control').find('.box .focus').removeClass('focus');//vod 화면 전환
								$("#wrap").removeClass().addClass(this["Video"].rootID.replace('#',''));//vod 화면 전환
								this.setDelegate(this["Qmenu"]);
								this["ChannelSub"].setFocus(CJ.Channel.lastID);
								this["ProgramSub"].setFocus(CJ.Program.lastID);
								this["EpisodeSub"].setFocus(CJ.Episode.lastID);
							},
							vod: function(){
								//현재 에피소드 선택
								/////$(this["EpisodeSub"].rootID + ' .scroll li').removeClass('select');
								$("li:nth-child(2)", this["Video"].rootElem).addClass('focus');//pause 버튼 포커스
								$(this["Qmenu"].rootID+" .menu").removeClass("hover m1 m2 m3 m4");//말풍선 제거
								$("#vod").removeClass().addClass('control');//vod 화면 전환
								this.setDelegate(this["Video"]);
							}
						});
						
						


	$(document).ready(function(){
		CJ.Scene["Video"] = new CJ.Video();
	});

})();